import requests,re,json,time
from requests.exceptions import RequestException
from multiprocessing import Pool
def get_one_page(url): # 
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
        }
        response = requests.get(url=url, headers=headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None
def parse_on_page(html):

    pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?poster-default".*?data-src="(.*?)".*?name">.*?<a'
    +'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S)
    items = re.findall(pattern, html)
    for item in items:
        yield {
            'index': item[0],
            'image': item[1],
            'title': item[2],
            'actor': item[3].strip()[3:],
            'item': item[4].strip()[5:],
            'score': str(item[5]) + str(item[6])
        }
def write_to_file(content):
    with open('maoyan.json', 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '\n')
def main(num):
    url = 'http://maoyan.com/board/4?offset=' + str(num)
    html = get_one_page(url)
    items = parse_on_page(html)
    for item in items:
        write_to_file(item)
    with open('maoyan.json', 'a', encoding='utf-8') as f:
        f.write('\n\n\n')
    print('写入--- %s页 ----完成！' % (int(num/10+1)))
    
if __name__ == '__main__':
    page = input('请输入您要获取猫眼电影的页数：')
    page = int(page)
    for i in range(0,page):
        time.sleep(2)
        main(i*10)
    # 多进程
    # pool = Pool() # 如果没有就创建一个进程池
    # pool.map(main, [i*10 for i in range(page)]) #map循环
